import {uniformCreditCodeEnum} from "../api/enum/uniformCreditCode";
// 统一社会信用代码

export const socialUniformCreditCode=(creditCode) => {
    if (creditCode.length != 18) {
        return  uniformCreditCodeEnum[0];
    }
    var sum = 0;
    var arr = new Array();
    arr = creditCode.split('');
    for (var i = 0; i < arr.length - 1; i++) {
        var c = arr[i];
        if (typeof c == 'undefined') {
            return uniformCreditCodeEnum[0];
        }
        var a = getMapC(c); //获得代码对应数值
        if (typeof a == 'undefined') {
            return uniformCreditCodeEnum[0]
        }
        var b = getMapW((i + 1).toString()); //获得当前位数权重
        if (typeof b == 'undefined') {
            return uniformCreditCodeEnum[0]
        }
        sum += a * b;
        if (typeof sum == 'undefined') {
            return uniformCreditCodeEnum[0];
        }
    }

    var mod = sum % 31;
    var res = getMapR((31 - mod).toString()); //计算校验码
    if (typeof res == 'undefined') {
        return uniformCreditCodeEnum[0];
    }
    if (arr[arr.length - 1] != res.toString()) {
        return uniformCreditCodeEnum[0];
    }
    return uniformCreditCodeEnum[1];
}

function getMapC(pa) {
    var result;
    switch (pa) {
        case '0':
            result = 0;
            break;
        case '1':
            result = 1;
            break;
        case '2':
            result = 2;
            break;
        case '3':
            result = 3;
            break;
        case '4':
            result = 4;
            break;
        case '5':
            result = 5;
            break;
        case '6':
            result = 6;
            break;
        case '7':
            result = 7;
            break;
        case '8':
            result = 8;
            break;
        case '9':
            result = 9;
            break;
        case 'A':
            result = 10;
            break;
        case 'B':
            result = 11;
            break;
        case 'C':
            result = 12;
            break;
        case 'D':
            result = 13;
            break;
        case 'E':
            result = 14;
            break;
        case 'F':
            result = 15;
            break;
        case 'G':
            result = 16;
            break;
        case 'H':
            result = 17;
            break;
        case 'J':
            result = 18;
            break;
        case 'K':
            result = 19;
            break;
        case 'L':
            result = 20;
            break;
        case 'M':
            result = 21;
            break;
        case 'N':
            result = 22;
            break;
        case 'P':
            result = 23;
            break;
        case 'Q':
            result = 24;
            break;
        case 'R':
            result = 25;
            break;
        case 'T':
            result = 26;
            break;
        case 'U':
            result = 27;
            break;
        case 'W':
            result = 28;
            break;
        case 'X':
            result = 29;
            break;
        case 'Y':
            result = 30;
            break;
    }
    return result;
}

function getMapW(pa) {
    var result;
    switch (pa) {
        case '1':
            result = 1;
            break;
        case '2':
            result = 3;
            break;
        case '3':
            result = 9;
            break;
        case '4':
            result = 27;
            break;
        case '5':
            result = 19;
            break;
        case '6':
            result = 26;
            break;
        case '7':
            result = 16;
            break;
        case '8':
            result = 17;
            break;
        case '9':
            result = 20;
            break;
        case '10':
            result = 29;
            break;
        case '11':
            result = 25;
            break;
        case '12':
            result = 13;
            break;
        case '13':
            result = 8;
            break;
        case '14':
            result = 24;
            break;
        case '15':
            result = 10;
            break;
        case '16':
            result = 30;
            break;
        case '17':
            result = 28;
            break;
    }
    return result;
}

function getMapR(pa) {
    var result;
    switch (pa) {
        case '0':
            result = '0';
            break;
        case '1':
            result = '1';
            break;
        case '2':
            result = '2';
            break;
        case '3':
            result = '3';
            break;
        case '4':
            result = '4';
            break;
        case '5':
            result = '5';
            break;
        case '6':
            result = '6';
            break;
        case '7':
            result = '7';
            break;
        case '8':
            result = '8';
            break;
        case '9':
            result = '9';
            break;
        case '10':
            result = 'A';
            break;
        case '11':
            result = 'B';
            break;
        case '12':
            result = 'C';
            break;
        case '13':
            result = 'D';
            break;
        case '14':
            result = 'E';
            break;
        case '15':
            result = 'F';
            break;
        case '16':
            result = 'G';
            break;
        case '17':
            result = 'H';
            break;
        case '18':
            result = 'J';
            break;
        case '19':
            result = 'K';
            break;
        case '20':
            result = 'L';
            break;
        case '21':
            result = 'M';
            break;
        case '22':
            result = 'N';
            break;
        case '23':
            result = 'P';
            break;
        case '24':
            result = 'Q';
            break;
        case '25':
            result = 'R';
            break;
        case '26':
            result = 'T';
            break;
        case '27':
            result = 'U';
            break;
        case '28':
            result = 'W';
            break;
        case '29':
            result = 'X';
            break;
        case '30':
            result = 'Y';
            break;
        case '31':
            result = '0';
            break;
    }
    return result;
}